<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>URL 参数解析器</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f0f8ff;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
        }

        h1 {
            color: #4CAF50;
            text-align: center;
        }

        label {
            font-size: 16px;
            color: #333;
        }

        input[type="text"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f1f1f1;
            border-radius: 5px;
            border: 1px solid #ddd;
        }

        .result p {
            margin: 5px 0;
            font-size: 14px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>URL 参数解析器</h1>
        <label for="urlInput">请输入带有参数的 URL：</label>
        <input type="text" id="urlInput" placeholder="例如: http://example.com?page=1&sort=desc">
        <button onclick="parseURL()">解析 URL</button>
        <div id="result" class="result"></div>
    </div>

    <script>
        var handleURLParam = function (url) {
            let res = {};
            if (url.lastIndexOf('?') === -1) return res;
            let param = decodeURIComponent(url.slice(url.lastIndexOf('?') + 1));
            let keys = param.match(/(\w+\=)|\=/g);
            keys && (keys = keys.map(k => k.replace(/\=/g, '')));
            let values = param.match(/(\=(\w+|\s*)\&)|(\=(\w+|\s*))/g);
            values && (values = values.map(v => v.replace(/\&|\=/g, '')));
            keys && keys.forEach((k, i) => (res[k] = values[i]));
            return res;
        };

        function parseURL() {
            let url = document.getElementById('urlInput').value;
            let result = handleURLParam(url);
            let resultDiv = document.getElementById('result');
            resultDiv.innerHTML = ''; // 清空之前的结果

            if (Object.keys(result).length === 0) {
                resultDiv.innerHTML = '<p>请输入有效的 URL 地址。</p>';
            } else {
                let html = '<h3>解析结果:</h3>';
                for (let key in result) {
                    html += `<p><strong>${key}:</strong> ${result[key]}</p>`;
                }
                resultDiv.innerHTML = html;
            }
        }
    </script>
</body>

</html>